package com.md.domain.service;


import com.md.application.assembler.ConfigAssembler;
import com.md.domain.model.po.GlobalConfigPO;
import com.md.domain.repository.GlobalConfigRepository;
import com.md.response.GlobalConfigResponse;
import com.md.value.GlobalConfigTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author 刘骄阳
 * 2024-12-15 1:47
 */
@Service
@RequiredArgsConstructor
public class ConfigDomainServices {
  private final GlobalConfigRepository globalConfigRepository;

  public List<GlobalConfigResponse> list() {
    return ConfigAssembler.buildGlobalConfigPO2Response(globalConfigRepository.selectAll());
  }

  public void updateConfig(final Long id, final String value) {
    GlobalConfigPO updatePO = new GlobalConfigPO();
    updatePO.setId(id);
    updatePO.setValue(value);
    globalConfigRepository.update(updatePO);
  }

  public void saveConfig(final GlobalConfigTypeEnum type, final String value) {
    GlobalConfigPO savePO = new GlobalConfigPO();
    savePO.setType(type.getCode());
    savePO.setValue(value);
    globalConfigRepository.insert(savePO);
  }

  public String selectConfig(final GlobalConfigTypeEnum type) {
    GlobalConfigPO selectOne = new GlobalConfigPO();
    selectOne.setType(type.getCode());
    return Optional.ofNullable(globalConfigRepository.select(selectOne)).map(GlobalConfigPO::getValue).orElse(null);
  }
}

